package com.swp.oa.sys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swp.oa.sys.dao.IAuthDao;
import com.swp.oa.sys.dao.IRoleAuthDao;
import com.swp.oa.sys.dao.IRoleDao;
import com.swp.oa.sys.dao.IUserRoleDao;
import com.swp.oa.sys.dto.domian.RoleDto;
import com.swp.oa.sys.dto.page.Combo;
import com.swp.oa.sys.pojo.Auth;
import com.swp.oa.sys.pojo.AuthExample;
import com.swp.oa.sys.pojo.Role;
import com.swp.oa.sys.pojo.RoleAuth;
import com.swp.oa.sys.pojo.RoleExample;
import com.swp.oa.sys.pojo.User;
import com.swp.oa.sys.pojo.UserExample;
import com.swp.oa.sys.pojo.UserRole;
import com.swp.oa.sys.pojo.UserRoleKey;
import com.swp.oa.sys.service.IRoleService;
import com.swp.oa.sys.util.Pager;
import com.swp.oa.util.DateUtil;
import com.swp.oa.util.UUIDGenerator;

@Service("roleService")
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private IRoleDao roleDao;
	@Autowired
	private IUserRoleDao userRoleDao;
	@Autowired
	private IRoleAuthDao roleAuthDao;
	@Autowired
	private IAuthDao authDao;	
	
	@Override
	public boolean save(RoleDto roleModel) {
		Role role = new Role();
		role.setRolename(roleModel.getName());
		role.setDescription(roleModel.getDescription());
		role.setRoleid(UUIDGenerator.getUUID());
		role.setCreatedate(new Date());
		role.setUpdatedate(role.getCreatedate());
		role.setVersion(1);
		roleDao.insertSelective(role);
		return true;
	}
	@Override
	public boolean saveRolePermissionItems(String roleId, String[] permissionItemArray) {
		roleAuthDao.deleteByRoleId(roleId);
		AuthExample example = new AuthExample();
		example.or().andParentauthidIsNull();
		List<Auth> authList = authDao.selectByExampleWithBLOBs(example);
		Set<String> roleAuthIds = new HashSet<String>();
		for (Auth a : authList) {
			roleAuthIds.add(a.getAuthid());
		}
		for (String  permissionId : permissionItemArray) {
			if(!roleAuthIds.contains(permissionId)){
				RoleAuth roleAuth = new RoleAuth();
				roleAuth.setAuthid(permissionId);
				roleAuth.setCreatedate(new Date());
				roleAuth.setRoleauthid(UUIDGenerator.getUUID());
				roleAuth.setUpdatedate(roleAuth.getCreatedate());
				roleAuth.setVersion(1);
				roleAuth.setRoleid(roleId);
				roleAuthDao.insertSelective(roleAuth);
			}
		}
		return true;
	}
	@Override
	public boolean deleteByPrimaryKey(String roleid) {
		roleAuthDao.deleteByRoleId(roleid);
		roleDao.deleteByPrimaryKey(roleid);
		return false;
	}
	@Override
	public boolean deleteUserRole(String userid, String roleid) {
		UserRoleKey key = new UserRoleKey();
		key.setUserid(userid);
		key.setRoleid(roleid);
		userRoleDao.deleteByPrimaryKey(key);
		return false;
	}
	
	@Override
	public void delete(String[] roleIdArray) {
		
		if(roleIdArray.length > 0){
			List<String> ids = new ArrayList<String>();
			for (String id : roleIdArray) {
				ids.add(id);
				roleAuthDao.deleteByRoleId(id);
			}
			RoleExample example = new RoleExample();
			example.or().andRoleidIn(ids);
			roleDao.deleteByExample(example);			
		}
	}
	@Override
	public boolean update(RoleDto roleModel) {
		Role role = new Role();
		role.setRoleid(roleModel.getId());
		role.setRolename(roleModel.getName());
		role.setDescription(roleModel.getDescription());
		role.setUpdatedate(new Date());
		roleDao.updateByPrimaryKeySelective(role);
		return false;
	}
	@Override
	public List<RoleDto> getAllRole() {
		List<RoleDto> roleModels = new ArrayList<RoleDto>();
		List<Role> roles = roleDao.selectAllList();
		if(roles != null){
			for (Role role : roles) {
				roleModels.add(this.pojoToModel(role));
			}
			return roleModels;
		}
		return null;
	}
	@Override
	public RoleDto getRoleByRoleId(String roleId) {
		Role role = roleDao.selectByPrimaryKey(roleId);
		if(role != null){
			return this.pojoToModel(role);
		}
		return null;
	}
	@Override
	public List<Auth> selectAuthsByRoleId(String roleid) {
		Role role = roleAuthDao.selectAuthsByRoleId(roleid);
		List<Auth> auths = role.getAuths();
		return auths;
	}

	RoleDto pojoToModel(Role role) {
		RoleDto model = new RoleDto();
		model.setId(role.getRoleid());
		model.setName(role.getRolename());
		model.setDescription(role.getDescription());
		model.setPermissionJsonData("");
		model.setCreateDate(DateUtil.dateFormat(role.getCreatedate()));
		return model;
	}

	@Override
	public Pager getRoleList(int pageIndex, int pageSize) {
		
		PageHelper.startPage(pageIndex,pageSize); 
		List<Role> list = roleDao.selectAllListWithBLOBs();
		if(list != null && list.size() > 0){
			PageInfo page = new PageInfo(list);
			Long totalCount = page.getTotal();
			List<RoleDto> roleList = new ArrayList<RoleDto>();
			for (Role r : list) {
				RoleDto role = this.pojoToModel(r);
				role.setPermissionJsonData("");
				roleList.add(role);
			}
			Pager pager = new Pager(pageIndex, pageSize, totalCount, roleList);
			return pager;			
		}
		return null;
	}
	
	public Page<Role> getRoleListByPage(int pageIndex, int pageSize) {
		PageHelper.startPage(pageIndex,pageSize); 
		
		return null;
	}
	
	@Override
	public boolean existRole(RoleDto roleModel) {
		if(roleModel.getName() != null){
			RoleExample example = new RoleExample();
			example.or().andRolenameEqualTo(roleModel.getName());
			List<Role> roles = roleDao.selectByExample(example);
			if(roles != null && roles.size() > 0){
				return true;
			}
		}
		return false;
	}
	@Override
	public List<Combo> getRoleComboData(String userId) {
		User user = userRoleDao.selectRolesByUserId(userId);
		String userRoleId = "";
		if(user != null && user.getRoles() != null){
			List<Role> roleList = user.getRoles();
			userRoleId = roleList.get(0).getRoleid();
		}
		List<Combo> comboList = new ArrayList<Combo>();
		List<Role> roles = roleDao.selectAllList();
		Combo combo = null;
		if(roles != null){
			for (Role role : roles) {
				combo = new Combo();
				combo.setText(role.getRolename());
				combo.setValue(role.getRoleid());
				if(!userRoleId.equals("") && role.getRoleid().equals(userRoleId)){
					combo.setSelected(true);
				}
				comboList.add(combo);
			}
			return comboList;
		}
		return null;
	}
	


}
